package com.fpmsa.officeweb.controller;

import com.fpmsa.officeweb.constant.OWConstant;
import com.fpmsa.officeweb.core.base.controller.BaseController;
import com.fpmsa.officeweb.core.exception.BizExceptionEnum;
import com.fpmsa.officeweb.core.exception.GunsException;
import com.fpmsa.officeweb.core.util.ToolUtil;
import com.fpmsa.officeweb.model.Ask;
import com.fpmsa.officeweb.service.IAskService;
import com.fpmsa.officeweb.service.OfficeWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * @Auther: Huang Yuehong
 * @Description: 用户相关控制器
 * @Date: 2018/7/1 19:11
 */

@Controller
@RequestMapping(value = "/user")
public class webUserController extends BaseController {

    private String PREFIX = "/templates/user/";

    @Autowired
    private OfficeWebService officeWebService;

    /**
     * 跳转到我要提问
     * @return
     */
    @RequestMapping("/toAsk")
    public String toAsk(HttpServletRequest request, Model model){

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "user_ask.html";
    }


    /**
     * 官网--我要提问，用户提交提问信息
     * @return
     */
    @RequestMapping("/ask")
    @ResponseBody
    public Object userAsk(Ask ask){
        if (ToolUtil.isEmpty(ask)){
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }

        if (ToolUtil.isEmpty(ask.getId())){

            ask.setCreateTime(new Date());
            ask.setUpdateTime(new Date());

            ask.insert();
        }


        return SUCCESS_TIP;
    }

    /**
     * 跳转到“我要投稿”
     * @param model
     * @return
     */
    @RequestMapping("/toSubmit")
    public String toSubmit(HttpServletRequest request,Model model){

        // 往页面的model对象里面添加“关键词”、“描述”、“title”、下拉框选项等属性,用request判断请求的来源（pc/移动端/微信端）
        officeWebService.addAttrForModel(request,model);

        return PREFIX + "user_submit.html";
    }
}
